package com.example.springbatch.common;

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Liaopan on 2018/9/12.
 */
public class KafkaReader<T> implements ItemReader {

    private List<T> record = new ArrayList<>();

    public void setRecord(T record){
        this.record.add(record);
    }

    public void setRecord(List<T> record){
        this.record.addAll(record);
    }

    @Override
    public Object read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        System.out.println("KafKa reader :" + record);
        while (this.record.isEmpty()) {
            Thread.sleep(5000);
        }

        return this.record.remove(0);
    }
}
